package com.company;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class test3 {
    public static void main(String[] args) {
        try {
//            test3.updateImage("D:\\图片素材\\210929\\kuangjia\\VW Golf 7_120_187_550_440_.png","D:\\图片素材\\210929\\aa\\1.png");
            test3.resizePNG("D:\\图片素材\\210929\\kuangjia\\VW Golf 7_120_187_550_440_.png","D:\\图片素材\\210929\\aa\\2.png",430,253,true);
           // test3.updateImage("D:\\图片素材\\210929\\ui\\1.png","D:\\图片素材\\210929\\aa\\1.png");
            //test3.margeImage("D:\\图片素材\\210929\\kuangjia\\VW Golf 7_120_187_550_440_.png","D:\\图片素材\\210929\\aa\\1.png","D:\\图片素材\\210929\\ui\\3.PNG");
        }catch (Exception e){

        }

    }
    public static void margeImage(String path1,String path2,String outPath) throws IOException {
        File file = new File(path1);
        Image img = ImageIO.read(file);
//水印宽度
        int width = img.getWidth(null);
//水印高
        int height = img.getHeight(null);
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.drawImage(img.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        ImageIcon imgIcon = new ImageIcon(path2);
//宽
        int imgIconWidth = imgIcon.getIconWidth();
//高
        int imgIconHeight = imgIcon.getIconHeight();
        Image con = imgIcon.getImage();
//透明度
        float clarity = 1.0f;
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, clarity));
//水印的位置
        g.drawImage(con, (width-imgIconWidth)/4, (height-imgIconHeight)/4, null);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        g.dispose();
        File sf = new File(outPath);
// 保存图片
        ImageIO.write(bi, "png", sf);


    }
    public static void updateImage(String inPath,String outPath) throws IOException{

        InputStream inputStream = new FileInputStream(new File(inPath));

        BufferedImage bufferedImage = ImageIO.read(inputStream);

        int height = bufferedImage.getHeight(); //图片的高
        int width = bufferedImage.getWidth();  //图片的宽

        int newWidth = 430;
        int newHeight = 253;
        //
//        if(wid>width){
//            double s = wid/width;
//            newHeight = (int) ((int) height*s);
//            newWidth = (int) ((int) width*s);
//        }
//        if(wid<width){
//            double s = width/wid;
//            newHeight = (int) ((int) height/s);
//            newWidth = (int) ((int) width/s);
//        }

        BufferedImage image = new BufferedImage(newWidth,newHeight, BufferedImage.TYPE_INT_BGR);

        Graphics garphics = image.createGraphics();
        garphics.drawImage(bufferedImage, 0, 0, newWidth, newHeight, null);

        OutputStream outputStream = new FileOutputStream(new File(outPath));

        JPEGImageEncoder j = JPEGCodec.createJPEGEncoder(outputStream);
        j.encode(image);

        outputStream.close();

    }

    public static void resizePNG(String fromFile, String toFile, int outputWidth, int outputHeight,boolean proportion) {

        try {

            File f2 = new File(fromFile);

            BufferedImage bi2 = ImageIO.read(f2);

            int newWidth;

            int newHeight;

// 判断是否是等比缩放

            if (proportion == true) {

// 为等比缩放计算输出的图片宽度及高度

                double rate1 = ((double) bi2.getWidth(null)) / (double) outputWidth + 0.1;

                double rate2 = ((double) bi2.getHeight(null)) / (double) outputHeight + 0.1;

// 根据缩放比率大的进行缩放控制

                double rate = rate1 < rate2 ? rate1 : rate2;

                newWidth = (int) (((double) bi2.getWidth(null)) / rate);

                newHeight = (int) (((double) bi2.getHeight(null)) / rate);

            } else {

                newWidth = outputWidth; // 输出的图片宽度

                newHeight = outputHeight; // 输出的图片高度

            }

            BufferedImage to = new BufferedImage(newWidth, newHeight,

                    BufferedImage.TYPE_INT_RGB);

            Graphics2D g2d = to.createGraphics();

            to = g2d.getDeviceConfiguration().createCompatibleImage(newWidth,newHeight,

                    Transparency.TRANSLUCENT);

            g2d.dispose();

            g2d = to.createGraphics();

            Image from = bi2.getScaledInstance(newWidth, newHeight, bi2.SCALE_AREA_AVERAGING);

            g2d.drawImage(from, 0, 0, null);

            g2d.dispose();

            ImageIO.write(to, "png", new File(toFile));

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

   /* public static void main(String[] args) throws IOException {

        System.out.println("Start");

        resizePNG("C:Documents and SettingsAdministrator桌面8d9e9c82d158ccbf8b31059319d8bc3eb035414e.jpg", "C:Documents and SettingsAdministrator桌面ell.png",200, 100,true);

        System.out.println("OK");

    }*/
}
